package life.kenny.community.controller;

import life.kenny.community.cache.HotTagsCache;
import life.kenny.community.dto.PagenationDto;
import life.kenny.community.dto.QuestionDTO;
import life.kenny.community.model.User;
import life.kenny.community.service.QuestionService;
import life.kenny.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.naming.Name;
import javax.print.attribute.standard.MediaSize;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Jay
 * @date 2021/9/9 10:28 上午
 */
@Controller
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private HotTagsCache hotTagsCache;

    @Autowired
    private QuestionService questionService;

    @GetMapping("/")
    public String index(HttpServletRequest request,
                        Model model,
                        @RequestParam(name = "page",defaultValue = "1")Integer page,
                        @RequestParam(name="size",defaultValue = "5")Integer size,
                        @RequestParam(name="tag", required = false)String tag,
                        @RequestParam(name = "search",required = false)String search,
                        @RequestParam(name = "sort",required = false)String sort)
    {

        PagenationDto Pagenations = questionService.list(page, size,tag,search,sort);
        List<String> hots = hotTagsCache.getHots();
        model.addAttribute("pagination", Pagenations);
        model.addAttribute("tag", tag);
        model.addAttribute("search", search);
        model.addAttribute("sort", sort);
        model.addAttribute("tags", hots);
        return "index";
    }
}
